package fr.imag.ufrima.ctte.gui.windows;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;
import fr.imag.ufrima.ctte.events.ImagCtteListener;
import fr.imag.ufrima.ctte.events.TaskDiagramAdapter;
import fr.imag.ufrima.ctte.events.TaskDiagramEvent;
import fr.imag.ufrima.ctte.gui.editors.Editor;
import fr.imag.ufrima.ctte.gui.editors.TaskDiagramEditor;
import fr.imag.ufrima.ctte.gui.menus.MainMenuBar;
import fr.imag.ufrima.ctte.gui.toolbars.MainToolBar;
import fr.imag.ufrima.ctte.gui.views.DiagramPropertiesSubView;
import fr.imag.ufrima.ctte.gui.views.MapSubView;
import fr.imag.ufrima.ctte.gui.views.PropertiesView;
import fr.imag.ufrima.ctte.gui.views.TaskPropertiesSubView;
import fr.imag.ufrima.ctte.gui.views.TaskTreeSubView;
import fr.imag.ufrima.ctte.gui.views.ToolsView;
import fr.imag.ufrima.ctte.gui.views.View;

/**
 * Main : the main window of the application.
 * 
 * @author Bassem Debbabi
 * 
 */
public class Main extends JFrame {
	private ImagCTTE imagCTTE;	
	/**
	 * Constructor
	 */
	public Main(ImagCTTE imagCTTE){
		super("ImagCTTE [Bassem DEBBABI & Yassine CHAOUFE]");
		this.imagCTTE = imagCTTE;
		InitComponents();					
		
		// listening to task diagram events
		imagCTTE.addImagCtteListener(new ImagCtteAdapter() {
			public void newTaskDiagram(ImagCtteEvent event) {											
				//=== NEW TASK =====================				
				if (getImagCTTE() != null){
					backPanel.setVisible(true);
				}
				//==================================			
			}				
			public void closeTaskDiagram(ImagCtteEvent event) {
				//=== NEW TASK =====================				
				if (getImagCTTE() != null){
					if (getImagCTTE().getTaskDiagramCount() == 0)
						backPanel.setVisible(false);
				}
				//==================================
			}
		});
		// TODO it is just for test purpose.
		imagCTTE.newTaskDiagram();
		// show the main window
		this.setVisible(true);
		
	}	
	/**
	 * Initialize components
	 */
	protected void InitComponents(){
		// splitpane
        splitPane1 = new javax.swing.JSplitPane();		        
        splitPane2 = new javax.swing.JSplitPane();                       
        // views
        propertiesView = new PropertiesView(imagCTTE);
        toolsView = new ToolsView(imagCTTE);
        
        // Editors
        taskDiagramEditor = new TaskDiagramEditor(imagCTTE);
        
		// main window properties		
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ImagCTTE   [Bassem DEBBABI & Yassine CHAOUFE]");
        setSize(new Dimension(800,600));
		setPreferredSize(new Dimension(800,600));
        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(null);

		// components creation
		menuBar = new MainMenuBar(imagCTTE);
        mainToolBar = new MainToolBar(imagCTTE);                        
                
        setJMenuBar(menuBar);
		
        add(mainToolBar, java.awt.BorderLayout.PAGE_START);
        
        backPanel = new JPanel();
        backPanel.setLayout(new BorderLayout());
        //backPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
                
        backPanel.setVisible(false);
        
     // splitPane1
        splitPane1.setDividerLocation(200);
        //splitPane1.setOneTouchExpandable(true);        
        backPanel.add(splitPane1, BorderLayout.CENTER);        
        
        // splitePane2
        splitPane2.setDividerLocation(310);
        //splitPane2.setOneTouchExpandable(true);
        splitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
        splitPane1.setLeftComponent(splitPane2);               
        
        // tabbed Panes
        //taskDiagramEditor = new TaskDiagramEditor(taskDiagram, tabbedPaneDiagram);		        
        //tabbedPaneDiagram.add(taskDiagramEditor.getTitle(), taskDiagramEditor);
        		        
        splitPane1.setRightComponent(taskDiagramEditor);        
        
        //updatePropertiesView(new DiagramPropertiesPanel(imagCTTE));        
        
        //tabbedPaneTree.addTab(taskTreeView.getTitle(), taskTreeView);
        //tabbedPaneTree.addTab(mapView.getTitle(), mapView);
        splitPane2.setLeftComponent(propertiesView);                   
        splitPane2.setRightComponent(toolsView);
        
        getContentPane().add(backPanel);
                
		pack();
	}
	/**
	 * Components
	 */
	// Menus
	private MainMenuBar menuBar;
	// ToolBars
    private MainToolBar mainToolBar;
    // Panels
    private javax.swing.JPanel backPanel;
    // Splites
    private javax.swing.JSplitPane splitPane1;
    private javax.swing.JSplitPane splitPane2;       
    // Views
    private View propertiesView;
    private View toolsView;
    // Editors    
    private Editor taskDiagramEditor;
    
	public ImagCTTE getImagCTTE() {
		return imagCTTE;
	}
	public void setImagCTTE(ImagCTTE imagCTTE) {
		this.imagCTTE = imagCTTE;
	}
    
    
}
